package com.reborn.mall.api.service.impl;

import com.reborn.mall.api.model.entity.ProductAttribute;
import com.reborn.mall.api.model.entity.ProductAttributeCategory;
import com.reborn.mall.api.service.ProductAttributeCategoryService;
import com.reborn.mall.api.service.ProductAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * TODO: Here is description
 *
 * @author wu.yue
 * @since 2020/2/1
 */
@Service
public class ProductAttributeServiceImpl extends BaseServiceImpl<ProductAttribute> implements ProductAttributeService {

    @Autowired
    private ProductAttributeCategoryService categoryService;

    @Override
    public ProductAttribute insertSelective(ProductAttribute entity) {
        ProductAttributeCategory category = categoryService.findById(entity.getProductAttributeCategoryId());
        if (entity.getType() == 0) {
            category.setAttributeCount(category.getAttributeCount() + 1);
        } else {
            category.setParamCount(category.getParamCount() + 1);
        }
        categoryService.updateByIdSelective(category);
        return super.insertSelective(entity);
    }

    @Override
    public int deleteById(Long id) {
        ProductAttribute attribute = findById(id);
        ProductAttributeCategory category = categoryService.findById(attribute.getProductAttributeCategoryId());
        if (attribute.getType() == 0) {
            category.setAttributeCount(category.getAttributeCount() - 1);
        } else {
            category.setParamCount(category.getParamCount() - 1);
        }
        categoryService.updateByIdSelective(category);
        return super.deleteById(id);
    }
}
